#include "LockFile.h"

LockFile :: LockFile ( int fd ) {

	strcpy ( this->nombre,nombre );

	this->fl.l_type = F_WRLCK;
	this->fl.l_whence = SEEK_SET;
	this->fl.l_start = 0;
	this->fl.l_len = 0;
	this->fl.l_pid = getpid ();
	this->fd = fd;
}

int LockFile :: tomarLockLectura () {
	this->fl.l_type = F_RDLCK;
	int resultado = fcntl ( this->fd,F_SETLKW,&(this->fl) );
	return resultado;
}

int LockFile :: tomarLockEscritura () {
	this->fl.l_type = F_WRLCK;
	int resultado = fcntl ( this->fd,F_SETLKW,&(this->fl) );
	return resultado;
}

int LockFile :: liberarLock () {
	this->fl.l_type = F_UNLCK;
	int resultado = fcntl ( this->fd,F_SETLK,&(this->fl) );
	return resultado;
}

int LockFile :: escribir ( const char* buffer,int buffsize ) {
	lseek ( this->fd,0,SEEK_END );
	int resultado = write ( this->fd,buffer,buffsize );
	return resultado;
}

int LockFile :: leer ( char* buffer,int buffsize ) {
	int resultado = read ( this->fd, buffer, buffsize );
	return resultado;
}

LockFile :: ~LockFile () {
}
